package com.atguigu.product.controller;

import com.atguigu.param.*;
import com.atguigu.pojo.Product;
import com.atguigu.product.service.ProductService;
import com.atguigu.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("product")
public class ProductController {

    @Autowired
    ProductService productService;

    @PostMapping("promo")
    public R promo(@RequestBody @Validated ProductPromoParam productPromoParam, BindingResult result){

        if(result.hasErrors()){
            return R.fail("/product/promo参数异常！");
        }
        return productService.promo(productPromoParam.getCategoryName());
    }

    @PostMapping("hots")
    public R hots(@RequestBody @Validated ProductHotsParam productHotsParam,BindingResult result){

        if(result.hasErrors()){
            return R.fail("参数异常");
        }
        return productService.hots(productHotsParam);
    }

    @PostMapping("category/list")
    public R categoryList(){
        return productService.categoryList();
    }

    @PostMapping("bycategory")
    public R bycategory(@RequestBody @Validated ProductBycategoryParam productBycategoryParam,BindingResult result){

        if(result.hasErrors()){
            return R.fail("参数异常");
        }
        return productService.bycategory(productBycategoryParam);
    }

    @PostMapping("all")
    public R all(@RequestBody @Validated ProductBycategoryParam productBycategoryParam,BindingResult result){

        if(result.hasErrors()){
            return R.fail("参数异常");
        }
        return productService.bycategory(productBycategoryParam);
    }

    @PostMapping("detail")
    public R detail(@RequestBody Map<String,Integer> params){
        Integer productID = params.get("productID");
        return productService.detail(productID);
    }

    @PostMapping("pictures")
    public R pictures(@RequestBody Map<String,Integer> params){

        Integer productID = params.get("productID");
        return productService.pictures(productID);
    }

    @GetMapping("list")
    public List<Product> list(){
        return productService.list();
    }

    @PostMapping("search")
    public R search(@RequestBody ProductSearchParam productSearchParam){
        return productService.search(productSearchParam);
    }

    @PostMapping("ids")
    public R ids(@RequestBody ProductIdsParam productIdsParam){

        return productService.ids(productIdsParam);
    }

    @GetMapping("id/{product_id}")
    public Product id(@PathVariable("product_id") Integer productId){
        return productService.id(productId);
    }

    // 统计该分类下的商品个数
    @PostMapping("count")
    public Long count(@RequestBody Integer categoryId){
        return productService.count(categoryId);
    }

    @PostMapping("save")
    public R save(@RequestBody ProductSaveParam productSaveParam){

        return productService.save(productSaveParam);
    }

    @PostMapping("update")
    public R update(@RequestBody Product product){
        return productService.update(product);
    }

    @PostMapping("remove")
    public R remove(@RequestBody Integer productId){
        return productService.remove(productId);
    }
}
